#!/usr/bin/env python3
"""
Django数据插入脚本
用于向book_info和people_info表插入测试数据
"""

import os
import sys
import django
from datetime import datetime

# 设置Django环境
sys.path.append('/home/chengdaohu46/桌面/django_base/bookmanager')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookmanager.settings')
django.setup()

from book.models import BookInfo, PeopleInfo

def insert_book_data():
    """插入书籍数据"""
    books_data = [
        ('射雕英雄传', '1980-5-1', 12, 34, 0),
        ('天龙八部', '1986-7-24', 36, 40, 0),
        ('笑傲江湖', '1995-12-24', 20, 80, 0),
        ('雪山飞狐', '1987-11-11', 58, 24, 0)
    ]
    
    for book in books_data:
        b = BookInfo.objects.create(
            name=book[0],
            pub_date=datetime.strptime(book[1], '%Y-%m-%d').date(),
            readcount=book[2],
            commentcount=book[3],
            is_delete=book[4]
        )
        print(f'Created book: {b.name}')

def insert_people_data():
    """插入人物数据"""
    people_data = [
        ('郭靖', 1, 1, '降龙十八掌', 0),
        ('黄蓉', 0, 1, '打狗棍法', 0),
        ('黄药师', 1, 1, '弹指神通', 0),
        ('欧阳锋', 1, 1, '蛤蟆功', 0),
        ('梅超风', 0, 1, '九阴白骨爪', 0),
        ('乔峰', 1, 2, '降龙十八掌', 0),
        ('段誉', 1, 2, '六脉神剑', 0),
        ('虚竹', 1, 2, '天山六阳掌', 0),
        ('王语嫣', 0, 2, '神仙姐姐', 0),
        ('令狐冲', 1, 3, '独孤九剑', 0),
        ('任盈盈', 0, 3, '弹琴', 0),
        ('岳不群', 1, 3, '华山剑法', 0),
        ('东方不败', 0, 3, '葵花宝典', 0),
        ('胡斐', 1, 4, '胡家刀法', 0),
        ('苗若兰', 0, 4, '黄衣', 0),
        ('程灵素', 0, 4, '医术', 0),
        ('袁紫衣', 0, 4, '六合拳', 0)
    ]
    
    for person in people_data:
        p = PeopleInfo.objects.create(
            name=person[0],
            gender=person[1],
            book_id=person[2],
            description=person[3],
            is_delete=person[4]
        )
        print(f'Created person: {p.name}')

if __name__ == '__main__':
    try:
        print("开始插入书籍数据...")
        insert_book_data()
        
        print("\n开始插入人物数据...")
        insert_people_data()
        
        print("\n数据插入完成！")
        print(f"总共插入 {BookInfo.objects.count()} 本书籍")
        print(f"总共插入 {PeopleInfo.objects.count()} 个人物")
        
    except Exception as e:
        print(f"插入数据时发生错误: {e}")
        sys.exit(1)